package com.akkainaction.helloactor.circuitbreaker.actor;

import akka.actor.*;
import scala.concurrent.duration.Duration;
import java.util.concurrent.TimeUnit;

public class CircuitBreakerActor extends UntypedActor {

    private ActorRef workerChild;

    //我们设置了SupervisorStrategy.resume()的策略，让Actor在出现异常或超时的时候能继续运行
    private static SupervisorStrategy strategy = new OneForOneStrategy(20, Duration.create(1, TimeUnit.MINUTES), t -> SupervisorStrategy.resume());

    @Override
    public SupervisorStrategy supervisorStrategy() {
        return strategy;
    }

    @Override
    public void preStart() throws Exception {
        workerChild = getContext().actorOf(Props.create(WorkerActor.class), "workerActor");
    }

    @Override
    public void onReceive(Object msg) throws Exception {
        workerChild.tell(msg, getSender());
    }
}
